﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CoursePack;

namespace TheSchedulator.Forms
{
    public partial class frmSchedulator : Form
    {
        private Student m_Student;

        public frmSchedulator(Student _student)
        {
            InitializeComponent();
     
            this.m_Student = _student;
        }
        
        private void frmSemesterPicker_Load(object sender, EventArgs e)
        {

            // Load all days of the week
            foreach (DayOfWeek day in (DayOfWeek[])Enum.GetValues(typeof(DayOfWeek)))
            {
                if (day != DayOfWeek.Sunday && day != DayOfWeek.Saturday)
                    this.cmbDaysofWeek.Items.Add(day);
            }

            this.cmbDaysofWeek.SelectedIndex = 0;

            // Load all possible Ession : Winter, Fall and Summer
            foreach (ESession sess in (ESession[])Enum.GetValues(typeof(ESession)))
            {
                this.cmbSemesters.Items.Add(sess);
            }

            this.cmbSemesters.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.treeView1.SelectedNode != null)
            {
                UserControls.CourseChoice courseChoice = new UserControls.CourseChoice(this.treeView1.SelectedNode);
                courseChoice.OnRemoveCourse += courseChoice_OnRemoveCourse;
                this.flowLayoutPanel1.Controls.Add(courseChoice);
                this.flowLayoutPanel1.Visible = true;
            }
        }

        private void courseChoice_OnRemoveCourse(object sender, EventArgs e)
        {
            ((UserControls.CourseChoice)sender).OnRemoveCourse -= courseChoice_OnRemoveCourse;

            this.flowLayoutPanel1.Controls.Remove((Control)sender);
        }

        private void vistaButton1_Click(object sender, EventArgs e)
        {
            this.flowLayoutPanel1.Controls.Clear();

        }

        private void cmbSemesters_SelectedIndexChanged(object sender, EventArgs e)
        {
            #region Loading Courses
            this.treeView1.Nodes.Clear();
            this.flowLayoutPanel1.Controls.Clear();

            List<Course> Courses = new List<Course>();
            List<Department> Departments = new List<Department>();

            Departments.Add(this.m_Student.Degree.Department);
            Departments.Add(new Department(408, "ENGR", null));

            Courses = MySQLApi.GetCourses(new Session((int)this.numericUpDown1.Value, this.ESession), Departments);

            if (Courses.Count > 0)
            {

                TreeNode CourseNode = new TreeNode(Courses[0].ToString());
                TreeNode TutorialN, LabN, LectureN;

                foreach (Course course in Courses)
                {
                    string str1 = CourseNode.Text;
                    string str2 = course.ToString();
                    if (str1 != str2)
                    {
                        CourseNode = new TreeNode(course.ToString());
                        this.treeView1.Nodes.Add(CourseNode);
                    }

                    LectureN = new TreeNode(course.Lecture.ToString());
                    LectureN.Tag = course;

                    CourseNode.Nodes.Add(LectureN);


                    if (course.HasTutorial)
                    {
                        TutorialN = new TreeNode(course.Tutorial.ToString());
                        TutorialN.Tag = course;

                        if (course.HasLab)
                        {
                            LabN = new TreeNode(course.Lab.ToString());
                            LabN.Tag = course;
                            TutorialN.Nodes.Add(LabN);
                            LectureN.Nodes.Add(TutorialN);
                        }
                        else
                        {
                            LectureN.Nodes.Add(TutorialN);
                        }
                    }
                    else if (course.HasLab)
                    {
                        LabN = new TreeNode(course.Lab.ToString());
                        LabN.Tag = course;
                        LectureN.Nodes.Add(LabN);
                    }
                }
            }
            #endregion
        }

        private void btnFindCourse_Click(object sender, EventArgs e)
        {
            this.btnClearList_Click(null, null);
            List<CourseDescription> courses = this.m_Student.Degree.CourseSequence.CourseInSemester((int)this.nmYear.Value, (ESession)this.cmbSemesters.SelectedItem);

            if (courses != null)
            {
                foreach (CourseDescription cc in courses)
                {
                    foreach (TreeNode node in this.treeView1.Nodes)
                    {
                        string tmp = cc.CourseName + "  " + cc.CourseNumber;
                        string tmp2 = node.Text;

                        if (tmp == tmp2)
                        {
                            UserControls.CourseChoice courseChoice = new UserControls.CourseChoice(node);
                            courseChoice.OnRemoveCourse += courseChoice_OnRemoveCourse;


                            this.flowLayoutPanel1.Controls.Add(courseChoice);
                        }
                    }
                }
                this.flowLayoutPanel1.Visible = true;
            }
            else
            {
                this.lblTitle.Text = "No courses have been found for the specified Semester and Year. Make sure your course sequence has such a semester.";
                this.lblTitle.BackColor = Color.DarkRed;
                this.lblTitle.ForeColor = Color.White;
                this.timer1.Start();
            }
        }

        private void btnGenerateSchedule_Click(object sender, EventArgs e)
        {
            List<TreeNode> _CoursesList = new List<TreeNode>();

            List<Scheduler.Constraint> _ConstraintsList = new List<Scheduler.Constraint>();

            foreach(Control cc in this.flowLayoutPanel1.Controls)
            {
                if (cc.GetType() == typeof(UserControls.CourseChoice))
                {
                    _CoursesList.Add(((UserControls.CourseChoice)cc).Course);
                }
            }

            foreach(Scheduler.TimeConstraint constraint in this.listBox1.Items)
            {
                _ConstraintsList.Add(constraint);
            }

            ScheduleViewer attachedForm = new ScheduleViewer(this.m_Student, new Session((int)this.numericUpDown1.Value, this.ESession), this.treeView1.Nodes, _CoursesList, _ConstraintsList);
            
            attachedForm.ShowInTaskbar = false;
            attachedForm.TopLevel = true;
            attachedForm.ShowDialog();
                      
        }

        private void btnAddConstraint_Click(object sender, EventArgs e)
        {
            if (((this.dateTimePicker1.Value.TimeOfDay > this.dateTimePicker2.Value.TimeOfDay) && this.ckAllDay.Checked == false) || (this.cmbDaysofWeek.SelectedItem ==null))
            {
                this.lblTitle.Text = "Please check your inputs. The End Time must be greater than the Start time. ";
                this.lblTitle.BackColor = Color.DarkRed;
                this.lblTitle.ForeColor = Color.White;
                this.timer1.Start();
                return;
            }

            Scheduler.TimeConstraint timeconstraint;

            if (this.ckAllDay.Checked)
            {
                timeconstraint = new Scheduler.TimeConstraint(Scheduler.TypeTimeConstraint.dayOff, (DayOfWeek)this.cmbDaysofWeek.SelectedItem);
            }
            else
            {
                timeconstraint = new Scheduler.TimeConstraint(this.dateTimePicker1.Value.TimeOfDay, this.dateTimePicker2.Value.TimeOfDay, (DayOfWeek)this.cmbDaysofWeek.SelectedItem);
            }

            
            this.listBox1.Items.Add(timeconstraint);
        }

        private void ckAllDay_CheckedChanged(object sender, EventArgs e)
        {
            this.dateTimePicker1.Enabled = !ckAllDay.Checked;
            this.dateTimePicker2.Enabled = !ckAllDay.Checked;
        }

        private void btnClearList_Click(object sender, EventArgs e)
        {
            this.flowLayoutPanel1.Visible = false;
            this.flowLayoutPanel1.Controls.Clear();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
  
        }

        private void label19_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedIndex != -1)
            {
                this.listBox1.Items.Remove(this.listBox1.SelectedItem);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            this.lblTitle.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.lblTitle.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.lblTitle.Text = "Quick Selection : Choose your courses";
        }

        private ESession ESession
        {
            get
            {
                ESession session;

                if ((ESession)this.cmbSemesters.SelectedItem == ESession.Fall)
                    session = ESession.Fall;
                else if ((ESession)this.cmbSemesters.SelectedItem == ESession.Summer)
                    session = ESession.Summer;
                else
                    session = ESession.Winter;

                return session;
            }
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            this.cmbSemesters_SelectedIndexChanged(null, null);
        }

        private void nmYear_ValueChanged(object sender, EventArgs e)
        {

        }
    }
}
